//
//  OrderViewController.m
//  mhome
//
//  Created by YaSha_Tom on 2018/12/5.
//  Copyright © 2018年 刘春雷. All rights reserved.
//

#import "OrderViewController.h"
#import "OrderTableViewCell.h"
#import "OrderDetaileViewController.h"
#import "DataManager.h"
#import "UIImage+LX.h"

@interface OrderViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataSourceArray;
@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单列表";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, kSCREEN_WIDTH-20*kWidthScale, kSCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//隐藏UITableViewCell的分隔线
    [self.view addSubview:self.tableView];
    [self doNetworking];
    
}

- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",Domain,getAllOrderAndContrForCosApi];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:100];
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    [dic setValue:@"1" forKey:@"queryType"];
    [dic setValue:[de stringForKey:@"userid"] forKey:@"ownerId"];
    [[NetworkManager manager] sendPostRequestWithUrl:urlString paramenters:dic success:^(id responseObject) {
        NSLog(@"成功%@",responseObject);
        if([responseObject[@"code"]  isEqual: @1]){
           self.dataSourceArray = [DataManager getOrderListData:responseObject];
        }
        NSLog(@"=====%@",self.dataSourceArray);
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"失败%@",error);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSourceArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderCell"];
    if(cell == nil) {
        cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"orderCell"];
    }
    orderModel *model = self.dataSourceArray[indexPath.section];
    model.indexpath = indexPath;
    [cell setOrderData:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetaileViewController *orderDetaile = [[OrderDetaileViewController alloc]init];
    orderDetaile.model = self.dataSourceArray[indexPath.section];
    [self.navigationController pushViewController:orderDetaile animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 62*BIZ;
    } else {
        return 30*BIZ;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 20*BIZ;
    } else {
        return 5*BIZ;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
